<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\AdminGroup as AdminGroupModel;
use app\common\model\AdminAuthrule as AdminAuthruleModel;

use app\admin\validate\AdminGroup as AdminGroupValidate;
use think\exception\ValidateException;

use think\facade\View;
use think\facade\Request;

/**
 * 权限组
 * Class AuthGroup
 */
class AdminGroup extends Base
{
    /**
     * 权限组
     * @return mixed
     */
    public function index()
    {
        $auth_group_list = AdminGroupModel::select();
		
		View::assign('auth_group_list',$auth_group_list);
		return View::fetch('index');
    }

    /**
     * 添加权限组
     * @return mixed
     */
    public function add()
    {
        if (Request::isPost()) {

			$data	= Request::only(['title', 'status']);

            try {
				validate(AdminGroupValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (AdminGroupModel::create($data)) {
				$this->success('保存成功',url('admin/AdminGroup/index'));
			} else {
				$this->error('保存失败');
			}

        }

		return View::fetch('add');
    }

    /**
     * 编辑权限组
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

		if (Request::isPost()) {

			$data	= Request::only(['id', 'title', 'status']);
			
			if ($id == 1 && $data['status'] != 1) {
                $this->error('超级管理组不可禁用');
            }

            try {
				validate(AdminGroupValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (AdminGroupModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/AdminGroup/index'));
			} else {
				$this->error('保存失败');
			}

        }


        $admin_group = AdminGroupModel::find($id);
		
		View::assign('admin_group',$admin_group);
		return View::fetch('edit');
    }


    /**
     * 删除权限组
     * @param $id
     */
    public function delete($id)
    {
		
        if ($id == 1) {
            $this->error('超级管理组不可删除');
        }
		
		$admin_group = AdminGroupModel::find($id);

        if ($admin_group->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id)
    {

		View::assign('id',$id);
		return View::fetch('auth');
    }

    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     */
    public function getJson($id)
    {
        $auth_group_data = AdminGroupModel::find($id);
        $auth_rules      = explode(',', $auth_group_data->rules);
        $auth_rule_list  = AdminAuthruleModel::field('id,pid,title')->select();

        foreach ($auth_rule_list as $key => $value) {
            in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
        }

        return $auth_rule_list;
    }

    /**
     * 更新权限组规则
     * @param $id
     * @param $auth_rule_ids
     */
    public function updateAuthGroupRule($id, $auth_rule_ids = '')
    {

		if (Request::isPost()) {

			$group_data['rules'] = is_array($auth_rule_ids) ? implode(',', $auth_rule_ids) : '';
			
			if (AdminGroupModel::update($group_data, ['id' => $id])) {
				$this->success('授权成功',url('admin/AdminGroup/index'));
			} else {
				$this->error('授权失败');
			}

        }


    }
}